<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%--头部内容开始--%>
<jsp:include page="head.jsp"/>
<%--头部内容结束--%>
<%--中间内容开始--%>
<!--中间内容-->

<div class="layui-container fly-marginTop fly-user-main">
    <jsp:include page="manger_left.jsp"/>

    <div class="fly-panel fly-panel-user" pad20>

        <div class="layui-tab layui-tab-brief" lay-filter="user">
            <ul class="layui-tab-title" id="LAY_mine">
                <li data-type="mine-jie" lay-id="index" class="layui-this">轮播图管理</li>
            </ul>
            <div class="layui-tab-content">
                <%--表格--%>
                <table class="layui-hide" id="test" lay-filter="test"></table>

                <%--左上角工具栏--%>
                <script type="text/html" id="toolbarDemo">
                    <div class="layui-btn-container">
                        <button class="layui-btn layui-btn-sm"  lay-event="add" id="test7"><i class="layui-icon">&#xe681;</i>上传图片</button>
                        <button class="layui-btn layui-btn-sm" lay-event="getCheckData"><i class="layui-icon">&#xe640;</i>批量删除</button>
                    </div>
                </script><%--表格操作--%>
                <script type="text/html" id="barDemo">
                    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                </script>
                <script type="text/html" id="imgtmp">
                    <img src="${pageContext.request.contextPath}{{d.lPath}}" style="width: 108px;height: 50px">
                </script>

            </div>
        </div>

        <%--这里是弹出层的表单信息 表单的id用于表单的选择，style是在本页隐藏，只有点击添加才会弹出--%>
        <div class="layui-row" id="addImg" style="display:none;">

            <div class="layui-form layui-form-pane">
                <center>
                    <form:form action="${pageContext.request.contextPath}/lunbo/teacher/add.do" method="post">
                        <div class="layui-form-item">
                        </div>
                        <div class="layui-form-item">
                            <label for="lName" class="layui-form-label">名称</label>
                            <div class="layui-input-inline">
                                <input type="text" id="lName" name="lName" required lay-verify="required" autocomplete="off" class="layui-input">
                                <input type="hidden" id="lPath" name="lPath">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <button type="button" class="layui-btn  layui-btn-sm" id="uploadImg"><i class="layui-icon">&#xe681;</i>上传图片</button>
                        </div>

                        <div class="layui-form-item">
                            <button type="submit" class="layui-btn" lay-submit>立即添加</button>
                        </div>
                    </form:form>
                </center>
            </div>
            <div id="LAY_page1"></div>
        </div>

    </div>
</div>
<%--中间内容结束--%>
<%--底部内容开始--%>
<jsp:include page="bottom.jsp"/>
<%--底部内容结束--%>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-3.3.1.js" ></script>
<%--表格渲染--%>
<script>
    layui.use('table', function(){
        var table = layui.table;

        table.render({
            elem: '#test'
            ,url:'${pageContext.request.contextPath}/lunbo/teacher/admin_list.do'
            ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示'
                ,layEvent: 'LAYTABLE_TIPS'
                ,icon: 'layui-icon-tips'
            }]
            , id: 'test'
            ,title: '轮播图显示列表'
            ,cols: [[
                { fixed: 'left',type: 'checkbox',width:'10%'}
                ,{field:'lId', title:'ID',type:"numbers", width:'10%', unresize: true, align:'center'}
                ,{field:'lName', title:'标题', width:'15%', align:'center'}
                ,{field:'user', title:'上传用户', width:'15%', align:'center',templet: '<div>{{d.user.userName}}</div>' }
                ,{field:'lcreateTime', title:'上传时间', width:'20%', align:'center'}
                ,{field:'lPath', title:'图片', width:'20%', align:'center',templet: "#imgtmp"}
                ,{title:'操作', toolbar: '#barDemo', width:'10%', align:'center'}
            ]]
            ,page: true
            ,done:function(res,curr,count) {
                hoverOpenImg();//显示大图
                $('table tr').on('click',function(){
                    $('table tr').css('background','');
                });
            }
         ,text: {
                none: '暂无相关数据'
            }
        });
        //数据表格重载
        // 执行搜索，表格重载
        $('#search').on('click', function () {
            // 搜索条件
            var demoReload = $('#demoReload').val();
            table.reload('test', {
                method: 'get'
                , where: {
                    'keywords': demoReload
                }
                , page: {
                    curr: 1
                }
            });
        });

        $('.demoTable .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'getCheckData'://批量删除
                    var data = checkStatus.data;
                    if(data.length!=0){
                        var ids = [];//用户id
                        var lName=[];//用户名
                        for (var i = 0; i < data.length; i++) { //循环筛选出id
                            ids.push(data[i].lId);
                            lName.push(data[i].lName);
                        }
                        //发送删除请求
                        layer.confirm("确认要删除"+JSON.stringify(lName)+"吗，删除后不能恢复", { title: "删除确认" }, function(index){
                            senddel(ids);
                            return false;
                        });
                    }else{
                        layer.msg("您还未选择用户！",{icon: 5});
                    }
                    break;
                    case "add":
                        layer.open({
                            //layer提供了5种层类型。可传入的值有：0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）
                            type: 1,
                            title: "上传图片",
                            area: ['500px', '350px'],
                            content: $("#addImg")//引用的弹出层的页面层的方式加载上传图片表单
                        });
                        break;

            };
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    var ids=data.lId;
                    senddel(ids);
                    return false;
                });
            }else if(obj.event === 'detail'){
                window.location.href="${pageContext.request.contextPath}/blog/to_home.do?lId="+data.lId;
            }
        });

        function senddel(ids){
            $.ajax({
                url: "${pageContext.request.contextPath}/lunbo/teacher/admin_delete.do",
                type: "post",
                dataType:'json',
                data: 'ids='+ids,
                success: function (result) {
                    if (result.msg == 200) {
                        layer.msg('删除成功');
                        table.reload('test',{});
                    }else{
                        layer.msg(result.msg);
                    }
                }
            });
        }

    });
    function hoverOpenImg(){
        var img_show = null; // tips提示
        $('td img').hover(function(){
            var kd=$(this).width();
            kd1=kd*5;          //图片放大倍数
            kd2=kd*5+30;       //图片放大倍数
            var img = "<img class='img_msg' src='"+$(this).attr('src')+"' style='width:"+kd1+"px;' />";
            img_show = layer.tips(img, this,{
                tips:[4, 'rgba(41,41,41,.5)']
                ,area: [kd2+'px']
            });
        },function(){
            layer.close(img_show);
        });
        $('td img').attr('style','max-width:70px;display:block!important');
    }

</script>
<%--上传轮播图--%>
<script>

        layui.use('upload', function(){
            console.log("进入use");
            var $ = layui.jquery
                ,upload = layui.upload;
            //上传轮播图
            upload.render({
                elem: '#uploadImg'
                ,url: '${pageContext.request.contextPath}/lunbo/teacher/uploadImg.do'
                ,done: function(res){
                    //如果上传失败
                    if(res.code == 0){
                        return layer.msg('上传失败！');
                    }
                    //上传成功
                    if(res.code > 0){
                        layer.msg('上传成功！')
                        //设置隐藏域的值
                        console.log("设置隐藏域的值--"+lPath);
                        $("#lPath").val(res.lPath);
                    }
                }

            });

        });


</script>
<%--底部内容结束--%>